3 * This is the MySQLi database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace Wikimedia\Rdbms
;
31 * Database abstraction object for PHP extension mysqli.
37 class DatabaseMysqli
extends DatabaseMysqlBase
{
40 * @return mysqli_result|bool
42 protected function doQuery( $sql ) {
43 return $this->getBindingHandle()->query( $sql );
47 * @param string $realServer
48 * @param string|null $dbName
50 * @throws DBConnectionError
52 protected function mysqlConnect( $realServer, $dbName ) {
53 if ( !function_exists( 'mysqli_init' ) ) {
54 throw $this->newExceptionAfterConnectError(
55 "MySQLi functions missing, have you compiled PHP with the --with-mysqli option?"
59 // Other than mysql_connect, mysqli_real_connect expects an explicit port
60 // and socket parameters. So we need to parse the port and socket out of
64 $hostAndPort = IP
::splitHostAndPort( $realServer );
66 $realServer = $hostAndPort[0];
67 if ( $hostAndPort[1] ) {
68 $port = $hostAndPort[1];
70 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
71 // If we have a colon and something that's not a port number
72 // inside the hostname, assume it's the socket location
73 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
76 $mysqli = mysqli_init();
77 // Make affectedRows() for UPDATE reflect the number of matching rows, regardless
78 // of whether any column values changed. This is what callers want to know and is
79 // consistent with what Postgres, SQLite, and SQL Server return.
80 $connFlags = MYSQLI_CLIENT_FOUND_ROWS
;
81 if ( $this->getFlag( self
::DBO_SSL
) ) {
82 $connFlags |
= MYSQLI_CLIENT_SSL
;
91 if ( $this->getFlag( self
::DBO_COMPRESS
) ) {
92 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
94 if ( $this->getFlag( self
::DBO_PERSISTENT
) ) {
95 $realServer = 'p:' . $realServer;
98 if ( $this->utf8Mode
) {
99 // Tell the server we're communicating with it in UTF-8.
100 // This may engage various charset conversions.
101 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
103 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
105 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
107 if ( $mysqli->real_connect(
125 protected function closeConnection() {
126 $conn = $this->getBindingHandle();
128 return $conn->close();
134 function insertId() {
135 $conn = $this->getBindingHandle();
137 return (int)$conn->insert_id
;
143 function lastErrno() {
144 if ( $this->conn
instanceof mysqli
) {
145 return $this->conn
->errno
;
147 return mysqli_connect_errno();
154 protected function fetchAffectedRowCount() {
155 $conn = $this->getBindingHandle();
157 return $conn->affected_rows
;
161 * @param mysqli_result $res
164 protected function mysqlFreeResult( $res ) {
171 * @param mysqli_result $res
172 * @return stdClass|bool
174 protected function mysqlFetchObject( $res ) {
175 $object = $res->fetch_object();
176 if ( $object === null ) {
184 * @param mysqli_result $res
185 * @return array|false
187 protected function mysqlFetchArray( $res ) {
188 $array = $res->fetch_array();
189 if ( $array === null ) {
197 * @param mysqli_result $res
200 protected function mysqlNumRows( $res ) {
201 return $res->num_rows
;
205 * @param mysqli_result $res
208 protected function mysqlNumFields( $res ) {
209 return $res->field_count
;
213 * @param mysqli_result $res
217 protected function mysqlFetchField( $res, $n ) {
218 $field = $res->fetch_field_direct( $n );
220 // Add missing properties to result (using flags property)
221 // which will be part of function mysql-fetch-field for backward compatibility
222 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
223 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
224 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
225 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
226 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
227 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
228 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
229 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
230 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
236 * @param mysqli_result $res
240 protected function mysqlFieldName( $res, $n ) {
241 $field = $res->fetch_field_direct( $n );
247 * @param mysqli_result $res
251 protected function mysqlFieldType( $res, $n ) {
252 $field = $res->fetch_field_direct( $n );
258 * @param mysqli_result $res
262 protected function mysqlDataSeek( $res, $row ) {
263 return $res->data_seek( $row );
267 * @param mysqli|null $conn Optional connection object
270 protected function mysqlError( $conn = null ) {
271 if ( $conn === null ) {
272 return mysqli_connect_error();
279 * Escapes special characters in a string for use in an SQL statement
283 protected function mysqlRealEscapeString( $s ) {
284 $conn = $this->getBindingHandle();
286 return $conn->real_escape_string( (string)$s );
292 protected function getBindingHandle() {
293 return parent
::getBindingHandle();
298 * @deprecated since 1.29
300 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );